package com.alipay.demo.trade.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class GoodDateUtil {
	
	 /* 
     * 获取时间
     */  
	  public static Long getTime() {
		  return Instant.now().toEpochMilli()/1000;
	  }
	
	 /* 
     * 将时间转换为时间戳
     */    
    public static String dateToStamp(String s)  {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamp2(LocalDateTime s)  {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = s.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
        Date date = Date.from(zdt.toInstant());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        try {
//            date = simpleDateFormat.parse();
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        long ts = date.getTime();
        return ts;
    }

    public static String dateToStamp1(String s) throws ParseException{
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamps(String s) {
        long res =0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        try {
            date = simpleDateFormat.parse(s);
            res = date.getTime();


        } catch (ParseException e) {
            e.printStackTrace();
        }
        return res;
    }

    /* 
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    
    /* 
     * 将Date时间 转成  String时间
     */
    public static String DateToString(Date date){
    	 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	 return sdf.format(date);
    }
    
    /* 
     * 将String时间 转成  Date时间
     */
    public static Date DateStringToDate(String date) throws ParseException{
    	 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   	 
   	    return sdf.parse(date);
   }
    
    /* 
     * 将long时间 转成  Date时间
     */
    public static String longToDate(long lo){    

    	Date date = new Date(lo);   

    	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    

    	return sd.format(date);  

    	}
    
    
    public static void main(String[] args) throws ParseException {
    	System.out.println("当前时间："+DateToString(new Date()));
    	String dateString=GoodDateUtil.DateToString(new Date());
		String sjc=GoodDateUtil.dateToStamp(dateString);
		System.out.println("当前时间 时间戳："+sjc);
		String sjc2=GoodDateUtil.stampToDate("1289427561");
		System.out.println("当前时间："+sjc2);
		
		DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault());
		Long i=Instant.now().toEpochMilli()/1000;
		Long i1=(long) (1543195704*1000);
         System.out.println("longToDate="+longToDate(i1));
		
		
	}
    
  
   
}
